package com.suny.sf_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suny.DataDic.dto.DataDicUpdateStatusDTO;
import com.suny.DataDicVal.dto.DataDIcValUpdateStatusDTO;
import com.suny.DataDicVal.vo.DataDicValSelectVO;
import com.suny.sf_server.service.DataDicValService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.suny.DataDicVal.dto.DataDicValDTO;
import com.suny.DataDicVal.dto.DataDicValQueryDTO;
import com.suny.DataDicVal.vo.DataDicValVO;
import com.suny.result.PageDTO;
import com.suny.result.PageVO;
import com.suny.result.R;

import java.util.List;

@Slf4j
@Tag(name = "数据字典码值", description = "数据字典码值相关接口")
@RestController
@RequestMapping("/dataDicVal")
public class DataDicValController {
    @Autowired
    private DataDicValService dataDicValService;
    @Operation(summary = "添加数据字典码值")
    @PostMapping("/add")
    public R addDataDicVal(@Validated @RequestBody DataDicValDTO dataDicValDTO) {
        Boolean result=dataDicValService.addDataDicVal(dataDicValDTO);
        return result?R.OK( "添加成功"):R.FAIL( "添加失败");
    }
//    @Operation(summary = "分页查询数据字典码值")
//    @PostMapping("/{dicId}")
//    public R listDataDicVal(@Parameter(description = "字典ID") @PathVariable("dicId") Integer dicId, @RequestBody PageDTO pageDTO) {
//        Page<DataDicValVO> dataDicVal=dataDicValService.listDataDicVal(dicId,pageDTO);
//        return convert(dataDicVal);
//    }
    @Operation(summary = "参数查询数据字典码值")
    @PostMapping("/list")
    public R getListByQuery(@RequestBody DataDicValQueryDTO dto){
        Page<DataDicValVO> dataDicVal=dataDicValService.getListByQuery(dto);
        return convert(dataDicVal);
    }
    @Operation(summary = "修改数据字典码值状态")
    @PostMapping("/updateStatus")
    public R updateStatus(@Validated @RequestBody DataDIcValUpdateStatusDTO dto){
        Integer valId=dto.getValId();
        Integer status=dto.getStatus();
        Boolean result=dataDicValService.updateStatus(valId,status);
        return result?R.OK( "修改成功"):R.FAIL( "修改失败");
    }
    @Operation(summary = "下拉框获取可用字典码")
    @GetMapping("/select")
    public R getSelectByDicId(){
        List<DataDicValSelectVO> dataDicVal=dataDicValService.getSelectByDicId();
        return R.OK(dataDicVal);
    }

    private R convert(Page<DataDicValVO> dataDicVal) {
        PageVO<DataDicValVO> dicValVOPageVO = new PageVO<>();
        dicValVOPageVO.setCurrent(dataDicVal.getCurrent());
        dicValVOPageVO.setSize(dataDicVal.getSize());
        dicValVOPageVO.setTotal(dataDicVal.getTotal());
        dicValVOPageVO.setPages(dataDicVal.getPages());
        dicValVOPageVO.setRecords(dataDicVal.getRecords());
        return R.OK(dicValVOPageVO);
    }
}
